"""
Case Type   : 资源池化集群支持多链路
Case Name   : 主节点断开全链路,备节点断开链路1
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.查询集群状态
    2.主节点断开全链路，备节点断开链路1
    3.查看数据库状态
    4.恢复链路
    5.查看数据库状态
Expect      :
    1.数据库状态正常
    2.成功
    3.数据库状态正常
    4.成功
    5.数据库状态正常
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.standby_sh = CommonSH('Standby1DbUser')
        self.sta_root = Node(node="Standby1Root")
        self.ip_1 = macro.PRIMARY_LUN_IP.split()[0].strip()
        self.ip_2 = macro.PRIMARY_LUN_IP.split()[1].strip()
        self.ip_3 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.log.info("----获取主节点网络设备名称----")
        self.pri_network_card = []
        self.log.info("----获取网络设备名称----")
        interfaces = self.pri_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item or self.ip_2 in item:
                self.pri_network_card.append(item.split(' ')[1].split(':')[0])
        self.log.info(self.pri_network_card)
        self.log.info("----获取备节点网络设备名称----")
        self.sta_network_card = ''
        interfaces = self.sta_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_3 in item:
                self.sta_network_card = item.split(' ')[1].split(':')[0]
                break
        self.log.info(self.sta_network_card)

    def test_multipath(self):
        step = '--step1:查询集群状态 expect:成功--'
        self.log.info(step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

        step = '--step2:节点断开全链路，备节点断开链路1 expect:成功--'
        self.log.info(step)
        res = self.pri_root.sh(f'ifdown {self.pri_network_card[0]};'
                               f'ifdown {self.pri_network_card[1]}').result()
        self.log.info(res)
        self.assertEqual(
            2, res.count("successfully deactivated"), "执行失败" + step)
        res = self.sta_root.sh(f'ifdown {self.sta_network_card}').result()
        self.log.info(res)
        self.assertIn("successfully deactivated", res, "执行失败" + step)

        step = '--step3:查看数据库状态 expect:成功--'
        self.log.info(step)
        for i in range(60):
            self.log.info("----第" + str(i + 1) + "次查询集群状态----")
            time.sleep(2)
            cluster_detail = self.primary_sh.get_db_cluster_status("detail")
            self.log.info(cluster_detail)
            if "Degraded" in cluster_detail and "Primary Normal" in \
                    cluster_detail:
                break
            elif i == 59:
                cluster_res = "Degraded" or "Primary Normal" in cluster_detail
                self.assertTrue(cluster_res, "执行失败" + step)

        step = '--step4:恢复链路 expect:成功--'
        self.log.info(step)
        res = self.pri_root.sh(f'ifup {self.pri_network_card[0]};'
                               f'ifup {self.pri_network_card[1]}').result()
        self.log.info(res)
        self.assertEqual(
            2, res.count("successfully activated"), "执行失败" + step)
        res = self.sta_root.sh(f'ifup {self.sta_network_card};').result()
        self.log.info(res)
        self.assertIn("successfully activated", res, "执行失败" + step)
        start_res = self.primary_sh.exec_cm_ctl("start", '-n 1')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_start_node_success_msg, start_res,
                      '执行失败' + step)

        step = '--step5:查看数据库状态 expect:成功--'
        self.log.info(step)
        start_res = self.primary_sh.exec_cm_ctl("switchover", '-a')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_switchover_success_msg, start_res,
                      '执行失败' + step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

